package com.sr.examination.exam.common.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.sr.examination.R;
import com.sr.examination.exam.common.PayModel;
import com.sr.examination.exam.exercise.ExerciseActivity;
import com.tl.commonlibrary.tool.NumberUnit;
import com.tl.commonlibrary.ui.adapter.IAdapter;
import com.tl.commonlibrary.ui.adapter.ViewHolder;
import com.tl.commonlibrary.ui.beans.ExamSubBean;
import com.tl.commonlibrary.ui.beans.ExerciseListBean;

import java.util.List;

/**
 * Created by JongSung on 18/3/1.
 */

public class ExamSubAdapter extends IAdapter<ExamSubBean> {
    private PayModel payModel;
    private NumberUnit numberUnit;

    public ExamSubAdapter(Context context, List<ExamSubBean> datas) {
        super(context, datas, R.layout.item_sub_exam);
        numberUnit = new NumberUnit();
    }

    public void setPayModel(PayModel payModel) {
        this.payModel = payModel;
    }

    @Override
    protected void getView(ViewHolder viewHolder, ExamSubBean entity, int position, View convertView, boolean isConvert, int itemViewType, ViewGroup parent) {
        TextView examTitleTView = viewHolder.getView(R.id.examTitleTView);
        examTitleTView.setText(entity.getTitle());
        TextView exerciseCountTView = viewHolder.getView(R.id.exerciseCountTView);
        exerciseCountTView.setText(context.getString(R.string.exercise_count_pre, String.valueOf(entity.getItemNum())));
        Button startBtn = viewHolder.getView(R.id.startBtn);

        Button tryBtn = viewHolder.getView(R.id.tryBtn);
        startBtn.setTag(R.id.tag_id, entity);
        tryBtn.setTag(R.id.tag_id, entity);
        if (entity.canStart()) {
            startBtn.setText(context.getString(R.string.start_exam));
            startBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            tryBtn.setVisibility(View.GONE);
        } else {
            numberUnit.set(entity.getPrice());
            SpannableString ss = new SpannableString(context.getString(R.string.start_exam) + " " + numberUnit.setFormatDot(false).get2F());
            ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.base_yellow)), 2, ss.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            startBtn.setText(ss);
            startBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_coin_small, 0);
            tryBtn.setVisibility(View.VISIBLE);
        }
//        if (entity.canTry()) {
//            tryBtn.setVisibility(View.VISIBLE);
//        } else {
//            tryBtn.setVisibility(View.GONE);
//        }
        if (!isConvert) {
            startBtn.setOnClickListener(this);
            tryBtn.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        ExamSubBean bean = (ExamSubBean) v.getTag(R.id.tag_id);
        switch (v.getId()) {
            case R.id.startBtn:
                //如果未购买并且不是免费的
                if (!bean.canStart()) {
                    if (payModel != null) {
                        payModel.setPaperId(bean.getId());
                        numberUnit.set(bean.getPrice());
                        payModel.show(numberUnit.setFormatDot(false).get2F());
                    }
                } else {
                    ExerciseActivity.start(context, bean.getTitle(), bean.getId(), ExerciseListBean.TYPE_ALL);
                }
                break;
            case R.id.tryBtn:
                ExerciseActivity.start(context, bean.getTitle(), bean.getId(), ExerciseListBean.TYPE_TRAIL);
                break;
        }
    }

}
